package com.runda.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.bbjob.mapper.ExamineeScoresDetailMapper;
import com.bbjob.mapper.ExamineeScoresMapper;
import com.bbjob.mapper.QueChoiceBankMapper;
import com.bbjob.mapper.QueChoiceMapper;
import com.bbjob.mapper.QueOperationBankMapper;
import com.bbjob.mapper.QueOperationItemMapper;
import com.bbjob.mapper.QueOperationMapper;
import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.mapper.SysOrganizationStuMapper;
import com.bbjob.model.ExamineeScores;
import com.bbjob.model.ExamineeScoresDetail;
import com.bbjob.model.QueChoice;
import com.bbjob.model.QueChoiceBank;
import com.bbjob.model.QueOperation;
import com.bbjob.model.QueOperationBank;
import com.bbjob.model.QueOperationItem;
import com.bbjob.model.SysOrganization;
import com.bbjob.model.SysOrganizationStu;
import com.github.pagehelper.PageHelper;
import com.runda.query.ExamineeScoreDetailEntity;
import com.runda.query.QueryScoresDetail;
import com.runda.service.IExamineeScoresDetailService;


@Service
public class ExamineeScoresDetailService implements IExamineeScoresDetailService {

	@Resource
	private SysOrganizationMapper sysOrganizationMapper;
	@Resource
	private ExamineeScoresDetailMapper examineeScoresDetailMapper;
	@Resource
	private SysOrganizationStuMapper sysOrganizationStuMapper;
	@Resource
	private ExamineeScoresMapper examineeScoresMapper;
	@Resource
	private QueChoiceMapper queChoiceMapper;
	@Resource
	private QueOperationMapper queOperationMapper;
	@Resource
	private QueOperationItemMapper queOperationItemMapper;
	@Resource
	private QueChoiceBankMapper queChoiceBankMapper;
	@Resource
	private QueOperationBankMapper queOperationBankMapper;
	
	
	@Override
	public List<ExamineeScoresDetail> selectByParams(QueryScoresDetail detail,
			Integer id) {
		
		if (detail.getUniquecode() != null && !"".equals(detail.getUniquecode())) {
			Example organExample = new Example(SysOrganization.class);
			organExample.createCriteria().andEqualTo("uniqueCode", detail.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(organExample);
			
			if (organs.size() > 0) {
				detail.setOrganSchool(organs.get(0).getId());
			}
		}

		PageHelper.startPage(detail.getPageNum(), detail.getPageSize());
		
		Example example = new Example(ExamineeScoresDetail.class);
		example.createCriteria().andEqualTo("examineeScoresId", id);
		List<ExamineeScoresDetail> list = examineeScoresDetailMapper.selectByExample(example);
		
		return list;
	}


	@Override
	public void saveByScoreDetail(ExamineeScoresDetail detail) {
		
		// 根据学校唯一编码获取 学校id
		if (detail.getUniquecode() != null && !"".equals(detail.getUniquecode())) {
			Example organExample = new Example(SysOrganization.class);
			organExample.createCriteria().andEqualTo("uniqueCode", detail.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(organExample);
			
			if (organs.size() > 0) {
				detail.setOrganSchool(organs.get(0).getId());
			}
		}
		
		// 根据学号查询学生所在 系 年级 班
		Example stuExample = new Example(SysOrganizationStu.class);
		stuExample.createCriteria().andEqualTo("organSchool", detail.getOrganSchool()).andEqualTo("sno", detail.getsNo());
		List<SysOrganizationStu> list = sysOrganizationStuMapper.selectByExample(stuExample);
		if (list.size() > 0) {
			SysOrganizationStu stu = list.get(0);
			detail.setOrganDepart(stu.getOrganDepart());
			detail.setOrganGrade(stu.getOrganGrade());
			detail.setOrganClass(stu.getOrganClass());
		}
		
		// 根据学号查询考试成绩主表id
		Example scoresExample = new Example(ExamineeScores.class);
		scoresExample.createCriteria().andEqualTo("sNo", detail.getsNo()).andEqualTo("examInfoId", detail.getExamInfoId()).andEqualTo("qId", detail.getqId());
		List<ExamineeScores> scoresList = examineeScoresMapper.selectByExample(scoresExample);
		if (scoresList.size() > 0) {
			detail.setExamineeScoresId(scoresList.get(0).getId());
		}

		// 根据题目类型，类型下的分类，序号获取题目id
		if (detail.getqType() == 1) {
			Example choiceBankExample = new Example(QueChoiceBank.class);
			choiceBankExample.createCriteria().andEqualTo("bankId", detail.getqId()).andEqualTo("choiceNo", detail.getQueNo());
			List<QueChoiceBank> choiceBankList = queChoiceBankMapper.selectByExample(choiceBankExample);
			if (choiceBankList.size() > 0) {
				QueChoice choice = queChoiceMapper.selectByPrimaryKey(choiceBankList.get(0).getChoiceId());
				if (choice != null) {
					detail.setqStem(choice.getTitle());
					detail.setQueId(choice.getId());
					detail.setqRightAnswer(choice.getRightAnswer());
					detail.setqScore(choice.getScore() != null ? new BigDecimal(choice.getScore()) : null);
				}
			}
			
/*			Example choiceExample = new Example(QueChoice.class);
			choiceExample.createCriteria().andEqualTo("cNo", detail.getQueNo()).andEqualTo("bankId", detail.getqId());
			List<QueChoice> choiceList = queChoiceMapper.selectByExample(choiceExample);
			if (choiceList.size() > 0) {
				QueChoice choice = choiceList.get(0);
				detail.setqStem(choice.getcTitle());
				detail.setQueId(choice.getId());
				detail.setqRightAnswer(choice.getcRightAnswer());
				detail.setqScore(choice.getcScore() != null ? new BigDecimal(choice.getcScore()) : null);
			}*/
		} else if (detail.getqType() == 2) {
			Example opeBankExample = new Example(QueOperationBank.class);
			opeBankExample.createCriteria().andEqualTo("bankId", detail.getqId()).andEqualTo("operationNo", detail.getQueNo());
			List<QueOperationBank> opeBankList = queOperationBankMapper.selectByExample(opeBankExample);
			if (opeBankList.size() > 0) {
				QueOperation operation = queOperationMapper.selectByPrimaryKey(opeBankList.get(0).getOperationId());
				
				if (operation != null) {
					detail.setqStem(operation.getContent());
					detail.setQueId(operation.getId());
				}
				
				QueOperationItem opeItem = queOperationItemMapper.selectByPrimaryKey(detail.getOperationId());
				if (opeItem != null) {
					detail.setqRightAnswer(opeItem.getRightAnswer());
					detail.setqScore(opeItem.getItemScore() != null ? new BigDecimal(opeItem.getItemScore()) : null);
				}
			}
			
/*			Example opeExample = new Example(QueOperation.class);
			opeExample.createCriteria().andEqualTo("eNo", detail.getQueNo()).andEqualTo("bankId", detail.getqId());
			List<QueOperation> opeList = queOperationMapper.selectByExample(opeExample);
			if (opeList.size() > 0) {
				QueOperation operation = opeList.get(0);
				detail.setqStem(operation.geteContent());
				detail.setQueId(operation.getId());

				QueOperationItem opeItem = queOperationItemMapper.selectByPrimaryKey(detail.getOperationId());
				if (opeItem != null) {
					detail.setqRightAnswer(opeItem.getItemRightAnswer());
					detail.setqScore(opeItem.getItemScore() != null ? new BigDecimal(opeItem.getItemScore()) : null);
				}
			}*/
		}
		
		Example detailExample = new Example(ExamineeScoresDetail.class);
		Criteria c = detailExample.createCriteria();
		
		c.andEqualTo("examineeScoresId", detail.getExamineeScoresId()).andEqualTo("examInfoId", detail.getExamInfoId()).andEqualTo("qId", detail.getqId()).andEqualTo("sNo", detail.getsNo()).andEqualTo("queId", detail.getQueId()).andEqualTo("queNo", detail.getQueNo());
		if (detail.getOperationId() != null && !"".equals(detail.getOperationId())) {
			c.andEqualTo("operationId", detail.getOperationId());
		}
		
		List<ExamineeScoresDetail> details = examineeScoresDetailMapper.selectByExample(detailExample);
		if (details.size() > 0) {
			detail.setId(details.get(0).getId());
			detail.setUpdateTime(new Date());
			examineeScoresDetailMapper.updateByPrimaryKeySelective(detail);
		} else {
			detail.setCreateTime(new Date());
			
			examineeScoresDetailMapper.insert(detail);
		}
	}


	@Override
	public void saveClientScoreByEntity(ExamineeScoreDetailEntity entity) {
		List<ExamineeScoresDetail> list = entity.getList();
		
		for (int i = 0; i < list.size(); i++) {
			ExamineeScoresDetail detail = list.get(i);
			detail.setUniquecode(entity.getUniquecode());
			saveByScoreDetail(detail);
		}
	}
}
